package com.ruoyi.project.system.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.SmExtract;
import com.ruoyi.project.system.domain.po.SmNoticeBase;
import com.ruoyi.project.system.mapper.SysNoticeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 公告 信息操作处理
 */
@RestController
@RequestMapping("/notice")
public class SysNoticeController extends BaseController {

    @Autowired
    private SysNoticeMapper sysNoticeMapper;

    /**
     * 最新通知
     */
    @GetMapping("/index")
    public AjaxResult index() {
        Long userId = SecurityUtils.getUserId();
        return success(sysNoticeMapper.firstUnReadNotice(userId));
    }

    /**
     * 写入已读通知
     */
    @GetMapping("/read")
    public AjaxResult read(@NotNull Integer noticeId) {
        Long userId = SecurityUtils.getUserId();
        return toAjax(sysNoticeMapper.addNoticeRecord(userId, noticeId));
    }

    /**
     * 获取咨询列表
     */
    @PreAuthorize("@ss.hasPermi('system:notice:list')")
    @GetMapping("/list")
    public TableDataInfo list() {
        startPage();
        List<SmNoticeBase> list = sysNoticeMapper.selectSmNoticeList();
        return getDataTable(list);
    }

    /**
     * 修改咨询
     */
    @PreAuthorize("@ss.hasPermi('system:notice:edit')")
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SmNoticeBase noticeBase) {
        return toAjax(sysNoticeMapper.updateSmNotice(noticeBase));
    }

    /**
     * 新增通知公告
     */
    @PreAuthorize("@ss.hasPermi('system:notice:add')")
    @PostMapping()
    public AjaxResult add(@Validated @RequestBody SmNoticeBase noticeBase) {
        noticeBase.setCreateBy(SecurityUtils.getUsername());
        return toAjax(sysNoticeMapper.insertSmNotice(noticeBase));
    }

    @PreAuthorize("@ss.hasPermi('system:notice:remove')")
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Integer id) {
        return toAjax(sysNoticeMapper.deleteSmNoticeById(id));
    }

}
